package priv.zgh.forum.discuss.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import priv.zgh.forum.discuss.entity.Posts;
import priv.zgh.forum.discuss.service.PostsService;
import priv.zgh.forum.re.entity.ResourcesGame;
import priv.zgh.forum.utils.R;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZGH
 * @since 2022-03-26
 */
@CrossOrigin
@RestController
@RequestMapping("/discuss/posts")
public class PostsController {
    PostsService postsService;
    @Autowired
    public void setPostsService(PostsService postsService) {
        this.postsService = postsService;
    }

    @ApiOperation(value = "按照传入条件来进行分页查询信息.")
    @PostMapping(path = "pageCondition/{current}/{limit}")
    public R pageCondition(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) Posts query){
        Page<Posts> page = postsService.pageResourcesCondition(current,limit,query);
        return R.ok().data("total",page.getTotal()).data("rows",page.getRecords());
    }

    @ApiOperation(value = "根据权限ID来获数据.")
    @GetMapping(path = "/getInfo/{id}")
    public R getInfoById(@PathVariable String id){
        Posts posts = postsService.getById(id);
        return R.ok().data("posts",posts);
    }

    @ApiOperation(value = "按照传入ID删除.")
    @DeleteMapping(path = "/delete/{id}")
    public R remote(@ApiParam(name = "id",value = "ID",required = true) @PathVariable(name = "id") String id){
        boolean flush = postsService.removeById(id);
        if(flush){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation(value = "保存")
    @PostMapping(path = "/add")
    public R add(@RequestBody Posts posts){
        boolean save = postsService.save(posts);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @ApiOperation(value = "更新")
    @PostMapping(path = "/updated")
    public R update(@RequestBody Posts posts){
        boolean flush = postsService.updateById(posts);
        if(flush){
            return R.ok();
        }else{
            return R.error();
        }
    }
}

